package com.smzd.framework.utils;

import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import com.smzd.framework.sso.domain.SocialUser;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.system.domain.User;
import com.smzd.utils.SessionHolder;

public class MeetingSessionHolder {
    public static final String KEY_MEETING = "_meeting";
    public static final String KEY_PRIVILEGE = "_privilege";
    public static final String KEY_LOGIN_USER = "_login_user";// 登录认证结束，实际登录的用户
    public static final String KEY_MEETING_LIST = "_meetnigs_list";
    public static final String KEY_SOCIAL_USER = "_social_user";// 第三方登录的用户信息
    public static final String KEY_PREPARED_USER = "_prepared_user";// 与第三方登录相对应的本地用户信息，将转化为_login_user的

    public static User getLoginUser() {
        User user = (User) SessionHolder.getObject(KEY_LOGIN_USER);
        if (user == null) {
            user = getUserFromSecurityContext();
        }
        return user;
    }

    private static User getUserFromSecurityContext() {
        User user = null;
        SecurityContext sc = SecurityContextHolder.getContext();
        if (sc != null) {
            Authentication a = sc.getAuthentication();
            if (a != null) {
                Object principle = a.getPrincipal();
                if (principle instanceof User) {
                    user = (User) principle;
                }
            }
        }
        return user;
    }

    public static void setLoginUser(User user) {
        SessionHolder.setObject(KEY_LOGIN_USER, user);
    }

    public static Privilege getPrivilege() {
        return (Privilege) SessionHolder.getObject(KEY_PRIVILEGE);
    }

    public static void setPrivilege(Privilege privilege) {
        System.out.println("-->setPrivilege:" + privilege);
        SessionHolder.setObject(KEY_PRIVILEGE, privilege);
    }

    public static void setMeeting(Meeting meeting) {
        SessionHolder.setObject(KEY_MEETING, meeting);
    }

    public static Meeting getMeeting() {
        Meeting meeting = (Meeting) SessionHolder.getObject(KEY_MEETING);
        if (meeting == null) {
            User user = getUserFromSecurityContext();
            if (user != null) {
                meeting = user.getOwner();
            }
        }
        return meeting;
    }

    public static void setMeetings(List<Meeting> meeting) {
        SessionHolder.setObject(KEY_MEETING_LIST, meeting);
    }

    public static List<Meeting> getMeetings() {
        return SessionHolder.getObject(KEY_MEETING_LIST);
    }

    public static void setSocialUser(SocialUser remoteUser) {
        SessionHolder.setObject(KEY_SOCIAL_USER, remoteUser);
    }

    public static SocialUser getSocialUser() {
        return SessionHolder.getObject(KEY_SOCIAL_USER);
    }

    public static void setPreparedUser(User user) {
        SessionHolder.setObject(KEY_PREPARED_USER, user);
    }

    public static User getPreparedUser() {
        return SessionHolder.getObject(KEY_PREPARED_USER);
    }

}
